package com.jacobson.core.utils

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager


/**
 * ================================================
 *
 *  @author Wucz on 2018-08-23 10:40
 * ================================================
 */
object SoftInputMethodManager {
    fun hide(activity: Activity?) {
        getInputMethodManager(activity)?.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
    }

    fun hide(context: Context, view: View) {
        getInputMethodManager(context)?.hideSoftInputFromWindow(view.windowToken, 0)
    }

    fun isShowing(context: Context, view: View? = null): Boolean {
        val imm = getInputMethodManager(context)
        //isOpen若返回true，则表示输入法打开
        return view?.let { imm?.isActive(it) } ?: imm?.isActive == true
    }

    fun show(context: Context?, view: View) {
        getInputMethodManager(context)?.showSoftInput(view, 0)
    }

    private fun getInputMethodManager(context: Context?): InputMethodManager? {
        return context?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
    }
}